<template>
  <div class="login-page" :style="bacImage">
    <!-- 右上 -->
    <div class="login-icon" v-if="logoShow == 0 && imgs != ''">
      <img :src="imgsUrl" alt="" />
    </div>
    <!-- 左上 -->
    <div class="login">
      <img
        v-if="logoShow === 1 && imgs != ''"
        style="
          margin-left: 80%;
          background-size: 100% 100%;
          margin-bottom: 18px;
        "
        class="login-icon1"
        :src="imgsUrl"
        alt=""
      />
      <div
        class="title"
        :style="{
          background: 'url(' + url + ') no-repeat center center',
          'background-size': '100% 100%',
          height: width1,
        }"
      ></div>
    </div>

    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form box-shadow"
    >
      <div class="form-title">{{ $t("账号登录") }}</div>
      <!-- 账号 -->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          :placeholder="$t('请输入账号')"
        >
          <img slot="prefix" src="../assets/images/home/login-username.png" />
        </el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          :placeholder="$t('请输入密码')"
          @keyup.enter.native="handleLogin"
        >
          <img slot="prefix" src="../assets/images/home/login-password.png" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <img slot="prefix" src="../assets/images/home/login-code.png" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <!-- <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        >记住密码</el-checkbox
      > -->
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">{{ $t("登录") }}</span>
          <span v-else>{{ $t("登录中") }}...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getCodeImg } from "@/api/login";
import { updateMap } from "../api/system/config";
export default {
  name: "Login",
  data() {
    var User = (rule, value, callback) => {
      if (this.loginForm.username == "") {
        return callback(new Error(this.$t("请输入您的账号")));
      }
      callback();
    };
    var Password = (rule, value, callback) => {
      if (this.loginForm.password == "") {
        return callback(new Error(this.$t("请输入您的密码")));
      }
      callback();
    };
    return {
      codeUrl: "",
      // url: "",
      // width1: "100px",
      //动态背景图片
      bacImageUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          {
            trigger: "blur",
            validator: User,
          },
        ],
        password: [
          {
            trigger: "blur",
            validator: Password,
          },
        ],
      },
      loading: false,
      captchaOnOff: true, // 验证码开关
      redirect: undefined,
      ruleForm: {
        configValue: this.$store.state.permission.i18n,
        configId: "",
        configKey: "language",
        configName: "系统语言",
      },
      imgsUrl: "",
      imgs: "",
      logoShow: "",
    };
  },

  computed: {
    bacImage() {
      return {
        backgroundImage: "url(" + this.bacImageUrl + ")",
      };
    },
    ...mapState({
      url: (state) =>
        state.permission.i18ns === "en_US"
          ? require("../assets/englishPictures/logo/IntelligentInspecti.png")
          : require("../assets/images/home/login-title.png"),
      width1: (state) =>
        state.permission.i18ns === "en_US" ? "103px" : "46px",
    }),
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  async created() {
    await this.getRobotInfo();
    await this.getKanguage();
    this.getCode();
    await this.getCookie();
    this.getUiupdate();
  },

  methods: {
    // 获取系统配置
    getUiupdate() {
      updateMap().then((res) => {
        this.imgs = res.data["login.logo.file"].configValue;
        // this.imgsUrl = "http://172.17.2.205:8080" + this.imgs;
        this.imgsUrl =
          window.location.protocol +
          "//" +
          window.location.host +
          "/prod-api" +
          this.imgs;
        this.logoShow = res.data["login.logo.position"].configValue * 1;
      });
    },
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getKanguage() {
      this.$store.dispatch("GetI18nsLanguage", this.ruleForm).then((res) => {
        this.$i18n.locale = res.rows[0].configValue;
        localStorage.setItem("setLanguage", res.rows[0].configValue);
        this.ruleForm.configId = res.rows[0].configId;
      });
    },
    /**存储机器信息  */
    getRobotInfo() {
      this.$store
        .dispatch("GetGlobalRobotInfo")
        .then((res) => {
          let robotType = res.data.robot.robotType.slice(
            0,
            res.data.robot.robotType.indexOf("-") + 2
          );
          switch (robotType) {
            case "T9-W":
              this.bacImageUrl = require("@/assets/images/home/T9W.jpg");
              break;
            case "T9-C":
              this.bacImageUrl = require("@/assets/images/home/T9C.jpg");
              break;
            case "T9-R":
              this.bacImageUrl = require("@/assets/images/home/T9R.jpg");
              break;
            case "T3-F":
              this.bacImageUrl = require("@/assets/images/home/T3F.jpg");
              break;
            case "T3-J":
              this.bacImageUrl = require("@/assets/images/home/T3J.jpg");
              break;
            case "T3-C":
              this.bacImageUrl = require("@/assets/images/home/T3C.jpg");
              break;
            default:
              this.bacImageUrl = require("@/assets/images/home/default.jpg");
              break;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          // if (this.loginForm.rememberMe) {//记住密码解开
          Cookies.set("username", this.loginForm.username, { expires: 30 });
          Cookies.set("password", encrypt(this.loginForm.password), {
            expires: 30,
          });
          Cookies.set("rememberMe", this.loginForm.rememberMe, {
            expires: 30,
          });
          // } else {
          //   Cookies.remove("username");
          //   Cookies.remove("password");
          //   Cookies.remove("rememberMe");
          // }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: "/robot/monitor" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped vars="{ width1, url }">
.login-page {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-right: 300px;
  height: 100%;
  background-repeat: no-repeat;
  background-position: center, center;
  background-size: 100% 100%;
}
// 标题
.title {
  width: 534px;
  color: #fff;
  margin-left: 71%;
  background-size: 100% 100%;
}
// 表单
.login-form {
  width: 480px;
  min-height: 309px;
  margin-top: 56px;
  padding: 40px 60px;
  margin-left: 73%;

  .form-title {
    margin-bottom: 32px;
    font-size: 24px;
    font-weight: 400;
    color: #ffffff;
  }
  img {
    width: 24px;
    height: 24px;
    left: 17px;
  }
  .login-code {
    width: 33%;
    height: 58px;
    float: right;
    border-radius: 4px;
    img {
      width: 100%;
      height: 100%;
      cursor: pointer;
      vertical-align: middle;
    }
  }
}
.login-icon {
  position: absolute;
  left: 20px;
  top: 20px;
  width: 246px;
  height: 46px;
  img {
    width: 100%;
    height: 100%;
  }
}
.login-icon1 {
  width: 246px;
  height: 46px;
  text-align: center;
}

.login-code-img {
  height: 38px;
  border-radius: 4px;
}
::v-deep {
  .el-form-item {
    margin-bottom: 24px !important;
  }
}
::v-deep {
  .el-input--medium {
    .el-input__inner {
      height: 60px;
      line-height: 60px;
      padding-left: 50px;
      color: #fff;
      background-color: transparent !important;
      border: 1px solid rgba(255, 255, 255, 0.3);
    }
  }

  input::-webkit-input-placeholder {
    color: #fff;
    font-size: 16px;
  }
  .el-input__prefix {
    top: 16px;
    left: 17px;
  }
  // 按钮
  .el-button--medium {
    height: 60px;
    background-color: rgba(0, 209, 255, 0.2);
    border-radius: 4px;
    font-size: 18px;
    color: #fff;
  }
}
::v-deep .login-form .el-input__inner {
  height: 56px;
  line-height: 56px;
  padding-left: 50px;
  color: #fff;
  background-color: transparent !important;
  border: 1px solid rgba(255, 255, 255, 0.3);
}
</style>
